package com.shelpe.services.lexicon.simba;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import rx.Observable;

@FeignClient(name="${feign.simba.name}", url="${feign.simba.url}", fallback = RecommendClientFallback.class)
public interface RecommendClient {
	
	@RequestMapping(path = "/simba/taobao.simba.keywords.recommend.get", method = RequestMethod.GET)
	Observable<RecommendKeywordResponse> findRecommendKeyword(@RequestParam("nick") String tenantName,
			@RequestParam("adgroup_id") long adgroupID, @RequestParam("page_size")int pageSize, 
			@RequestParam("page_no")int pageNo, @RequestParam("order_by")String order);
	
	@Setter
	@Getter
	public static class RecommendKeywordResponse extends SimbaProxyBaseDto{
		@JsonProperty("simba_keywords_recommend_get_response")
		private RecommendKeywordRes res;
	}
	
	@Setter
	@Getter
	public static class RecommendKeywordRes{
		@JsonProperty("recommend_words")
		private RecommendWordPage page;
	}
	
	@Getter
	@Setter
	public static class RecommendWordPage{
		
		@JsonProperty("page_size")
		private int pageSize;
		@JsonProperty("total_item")
		private int totalItem;
		@JsonProperty("page_no")
		private int pageNo;
		@JsonProperty("recommend_word_list")
		private RecommendWords list;
		
	}
	
	@Getter
	@Setter
	public static class RecommendWords {
		@JsonProperty("recommend_word")
		private List<RecommendWord> words;
	}
	
	@Getter
	@Setter
	public static class RecommendWord{
		private String pertinence;//相关度
		private String word;
		@JsonProperty("pv")
		private String searchVolume;//搜索量
		@JsonProperty("average_price")
		private String averagePrice;//平均价格
	}
	
	@RequestMapping(path = "/simba/taobao.simba.insight.relatedwords.get", method = RequestMethod.GET)
	RelatedWordResponse findRelatedData(@RequestParam("bidword_list") String[] words, @RequestParam("number")int number, 
			@RequestHeader("tenantID") long tenantID);
	
	@Getter
	@Setter
	public static class RelatedWordResponse extends SimbaProxyBaseDto{
		@JsonProperty("simba_insight_relatedwords_get_response")
		private RelatedWordRes res;
	}
	
	@Getter
	@Setter
	public static class RelatedWordRes{
		@JsonProperty("related_words_result_list")
		private RelatedWordInsightRes insightRes;
	}
	
	@Getter
	@Setter
	public static class RelatedWordInsightRes{
		@JsonProperty("insight_related_words")
		private List<WordRelationDataList> dataList;
	}
	
	@Data
	public class WordRelationDataList {
		@JsonProperty("bidword")
		private String word;
		@JsonProperty("related_word_items_list")
		private RelatedWords relatedWords;
	}
	
	@Data
	public class RelatedWords {
		@JsonProperty("insight_related_word")
		private List<RelatedWord> words;
	}
	
	@Data
	public class RelatedWord{
		@JsonProperty("related_word")
		private String word;
		private String weight;
	}
	
	@Service
	@Slf4j
	public static class RecommendClientFallback implements RecommendClient{

		@Override
		public Observable<RecommendKeywordResponse> findRecommendKeyword(String tenantName, long adgroupID,
				int pageSize, int pageNo, String order) {
			log.error("fallback findRecommendKeyword: {}", adgroupID);
			return Observable.just(new RecommendKeywordResponse());
		}

		@Override
		public RelatedWordResponse findRelatedData(String[] words, int number, long tenantID) {
			log.error("fallback findRelatedData: {}", words != null?words:"null");
			return new RelatedWordResponse();
		}
		
	}
}
